package com.tiyende.kash.loop.ui.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.TiyendeApp
import com.tiyende.kash.loop.common.CommonDialog
import com.tiyende.kash.loop.databinding.TiyendeActivityPermissionListReasonItemBinding
import com.tiyende.kash.loop.databinding.TiyendeDialogPermissionReasonBinding
import com.tiyende.kash.loop.event.TiyendeEventPointType
import com.tiyende.kash.loop.utils.TiyendeDensityUtil
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2025/6/10 15:50
 * @Annotation：权限说明dialog
 */
class TiyendePermissionReasonDialog: CommonDialog {

    private lateinit var binding: TiyendeDialogPermissionReasonBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    override fun initTiyendeWindow() {
        val params = window?.attributes
        params?.width = TiyendeApp.app.getPhoneWidth() - TiyendeDensityUtil.dp2px(32f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogPermissionReasonBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initTiyendeView() {
    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.btDeny)
        setOnClickListener(binding.btAgree)
        binding.checkBoxPermission.setOnCheckedChangeListener { _, isChecked ->
            when (isChecked) {
                true -> {
                    binding.btAgree.isEnabled = true
                }
                false -> {
                    binding.btAgree.isEnabled = false
                }
            }
        }
    }

    override fun loadTiyendeData() {
    }

    override fun onTiyendeViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.btDeny -> {
                mCallback?.onTiyendePermissionDenyClick()
            }
            R.id.btAgree -> {
                mCallback?.onTiyendePermissionAgreeClick()
            }
        }
    }

    /**
     * 权限说明
     */
    fun showTiyendePermissionReasonItem(data: JSONObject){
        binding.imgView.setImageResource(data.optInt("icon"))
        binding.textTitle.text = data.optString("title")

        binding.reasonLayout.removeAllViews()
        val msgList = data.optJSONArray("msgList")
        msgList?.let {
            for (i in 0..< it.length()){
                val item = it.optJSONObject(i)
                val itemView = TiyendeActivityPermissionListReasonItemBinding.inflate(layoutInflater)
                val showTitle = item.optString("title")
                if (showTitle.isNotEmpty()){
                    itemView.textTitle.text = showTitle
                    itemView.textTitle.visibility = View.VISIBLE
                } else {
                    itemView.textTitle.visibility = View.GONE
                }
                itemView.textReason.text = item.optString("des")
                binding.reasonLayout.addView(itemView.root)
            }
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onTiyendePermissionDenyClick()
        fun onTiyendePermissionAgreeClick()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_PERMISSION_REASON_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_PERMISSION_REASON_DISMISS,eventRemark)
    }
}